#!/bin/sh
if [ ! -f configure.paths ]; then
  echo "No configure.paths found, please run ./configure" >&2
  exit 1
fi

. ./configure.paths

path_bin="$CONFIG_BINPATH"
path_lib="$CONFIG_LIBPATH"
path_include="$CONFIG_INCLUDEPATH"

if [ ! -d "$path_bin" ]; then
  mkdir "$path_bin"
fi

if [ ! -d "$path_lib" ]; then
  mkdir "$path_lib"
fi

if [ ! -d "$path_include" ]; then
  mkdir "$path_include"
fi

if [ ! -d "$path_include/grace" ]; then
  mkdir "$path_include/grace"
fi

echo "Installing library in $path_lib"
if [ -e /usr/bin/install_name_tool ]; then
  install_name_tool -id $path_lib/libgrace.dylib ../../lib/libgrace.dylib
fi
cp ../../lib/libgrace.* "$path_lib/"
echo "Installing headers in $path_include"
cp ../../include/grace/*.h "$path_include/grace/"

echo "Installing utilities in $path_bin"
if [ -d ../../util ]; then
  rm -f "${path_bin}/grace"
  cp ../../util/grace "${path_bin}/"
  mkdir -p "${path_bin}/util-grace"
  for tool in ../../util/mk*; do
    toolnm=$(echo "$tool" | sed -e "s@.*/@@")
    cp "$tool" "${path_bin}/util-grace/"
    rm -f "${path_bin}/${toolnm}"
    ln -s grace "${path_bin}/${toolnm}"
  done
fi
